package com.anjiplus.template.gaea.business.modules.quartz.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
* @ClassName:  HttpUtil
* @Description: java调用http请求form表单提交参数工具类
* @author:li
* @date:2019年8月27日
*/
public class HttpUtil {

	 public static JSONObject http(String url, Map<String, String> params,String method) {
	        URL u = null;
	        HttpURLConnection con = null;
	        //构建请求参数
	        StringBuffer sb = new StringBuffer();
	        StringBuffer result = new StringBuffer();

	        String body = "";
	        JSONObject j1 = new JSONObject();
	        if (params != null) {
	        	sb.append("{");
	            for (Entry<String, String> e : params.entrySet()) {
	                sb.append("\""+e.getKey()+"\"");
	                sb.append(":");
	                sb.append("\""+e.getValue()+"\""+",");
	            }
	            body = sb.toString().substring(0,sb.toString().length()-1);
	            body = body+"}";
	        }
	        // 尝试发送请求
	        try {
	            u = new URL(url);
	            con = (HttpURLConnection) u.openConnection();
	            // POST 只能为大写，严格限制，post会不识别
	            con.setRequestMethod(method);
	            con.setDoOutput(true);
	            con.setDoInput(true);
	            con.setUseCaches(false);
	            con.setRequestProperty("Content-Type","application/json");
	            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
	            osw.write(body);
	            osw.flush();
	            osw.close();
	            if (con.getResponseCode() == 200) {
	            	BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));// 发送http请求
	            	String line = reader.readLine();
	   			 // 循环读取流
	   			if(line == null || "".equals(line)) {

	   			 }else {
	   				 System.out.println(line+"不为空");
		   			 j1 = JSON.parseObject(line);
	   			 }
	   		 }

	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            if (con != null) {
	                con.disconnect();
	            }
	        }
            System.out.println("HttpUtil里面的buffer.toString()==="+j1);
	        return j1;
	    }


	 /**
	     * @return
	     * @Description HTTP GET请求
	     * @Param
	     **/
	    public static String doGet(String url1,Map<String, String> params,String method) {
	    	String body = "";
	        HttpURLConnection connection = null;
	        StringBuffer sb = new StringBuffer();
		    InputStream is = null;
		    BufferedReader br = null;
		    // 返回结果字符串
		    String result = null;
	    	if (params != null) {
	    		sb.append("?");
	            for (Entry<String, String> e : params.entrySet()) {
	                sb.append(e.getKey());
	                sb.append("=");
	                sb.append(e.getValue()+"&");
	            }
	            body = sb.toString().substring(0,sb.toString().length()-1);
	        }
	    	String httpurl = url1+body;
	        try {
	            // 创建远程url连接对象
	            URL url = new URL(httpurl);
	            // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
	            connection = (HttpURLConnection) url.openConnection();
	            // 设置连接方式：get
	            connection.setRequestMethod(method);
	            // 设置连接主机服务器的超时时间：15000毫秒
	            connection.setConnectTimeout(15000);
	            // 设置读取远程返回的数据时间：60000毫秒
	            connection.setReadTimeout(60000);
	            // 发送请求
	            connection.connect();
	            // 通过connection连接，获取输入流
	            if (connection.getResponseCode() == 200) {
	                is = connection.getInputStream();
	                // 封装输入流is，并指定字符集
	                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	                // 存放数据
	                StringBuffer sbf = new StringBuffer();
	                String temp = null;
	                while ((temp = br.readLine()) != null) {
	                    sbf.append(temp);
	                    sbf.append("\r\n");
	                }
	                result = sbf.toString();
	            }
	        } catch (MalformedURLException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            // 关闭资源
	            if (null != br) {
	                try {
	                    br.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }

	            if (null != is) {
	                try {
	                    is.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            // 关闭远程连接
	            connection.disconnect();
	        }
	        return result;
	    }


	}
